{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# week8\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 地理编码 \n",
    "#### 1.调用requests模块(相当于打开浏览器进行搜索，必要条件) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1.请求方法GET\n",
    "## 2.请求地理编码URL ：https://restapi.amap.com/v3/geocode/geo?parameters\n",
    "geo_url = \"https://restapi.amap.com/v3/geocode/geo?parameters\"\n",
    "## 3.请求的参数（payload）payload模板，可用python写任何API测试\n",
    "payload = {\n",
    "   \"key\":\"bd94443798009a9d75e89bae46f822b1\",\n",
    "   \"address\":\"广东省广州市从化区广州南方学院\"\n",
    "   }\n",
    "## 4.发送请求\n",
    "r = requests.get(url=geo_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'113.678280,23.628439'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['geocodes'][0]['location']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'113.678280,23.628439'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "广州南方学院_地理编码 = r.json()['geocodes'][0]['location']\n",
    "广州南方学院_地理编码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 逆地理编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# extensions=base（默认）\n",
    "# 1. 调用requests模块（google chrome 浏览器）\n",
    "import requests\n",
    "# 2. 请求方法 ： GET\n",
    "# 3. URL地址 ： https://restapi.amap.com/v3/geocode/geo?parameters\n",
    "regeo_url = \"https://restapi.amap.com/v3/geocode/regeo?parameters\"\n",
    "# 4. 准备参数，用于交换数据 payload\n",
    "payload = {\n",
    "    \"key\":\"bd94443798009a9d75e89bae46f822b1\",\n",
    "    \"location\":广州南方学院_地理编码\n",
    "}\n",
    "# 5. 发起请求\n",
    "r = requests.get(url=regeo_url, params=payload)    "
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': '1',\n",
       " 'regeocode': {'addressComponent': {'city': '广州市',\n",
       "   'province': '广东省',\n",
       "   'adcode': '440117',\n",
       "   'district': '从化区',\n",
       "   'towncode': '440117100000',\n",
       "   'streetNumber': {'number': '24号',\n",
       "    'location': '113.677267,23.629149',\n",
       "    'direction': '西北',\n",
       "    'distance': '129.943',\n",
       "    'street': '博学路'},\n",
       "   'country': '中国',\n",
       "   'township': '温泉镇',\n",
       "   'businessAreas': [[]],\n",
       "   'building': {'name': [], 'type': []},\n",
       "   'neighborhood': {'name': [], 'type': []},\n",
       "   'citycode': '020'},\n",
       "  'formatted_address': '广东省广州市从化区温泉镇博学路广州南方学院'},\n",
       " 'info': 'OK',\n",
       " 'infocode': '10000'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# extensions=base（默认）\n",
    "# 1. 调用requests模块（google chrome 浏览器）\n",
    "import requests\n",
    "# 2. 请求方法 ： GET\n",
    "# 3. URL地址 ： https://restapi.amap.com/v3/geocode/geo?parameters\n",
    "regeo_url = \"https://restapi.amap.com/v3/geocode/regeo?parameters\"\n",
    "# 4. 准备参数，用于交换数据 payload\n",
    "payload = {\n",
    "    \"key\":\"bd94443798009a9d75e89bae46f822b1\",\n",
    "    \"location\":广州南方学院_地理编码,\n",
    "    \"extensions\":\"all\", # 返回信息设置为可以选择的全部信息\n",
    "    \"poitype\":\"true\"  # 开启附件搜索POI\n",
    "}\n",
    "# 5. 发起请求\n",
    "r = requests.get(url=regeo_url, params=payload)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': '1',\n",
       " 'info': 'OK',\n",
       " 'infocode': '10000',\n",
       " 'regeocode': {'formatted_address': '广东省广州市从化区温泉镇博学路广州南方学院',\n",
       "  'addressComponent': {'country': '中国',\n",
       "   'province': '广东省',\n",
       "   'city': '广州市',\n",
       "   'citycode': '020',\n",
       "   'district': '从化区',\n",
       "   'adcode': '440117',\n",
       "   'township': '温泉镇',\n",
       "   'towncode': '440117100000',\n",
       "   'neighborhood': {'name': [], 'type': []},\n",
       "   'building': {'name': [], 'type': []},\n",
       "   'streetNumber': {'street': '博学路',\n",
       "    'number': '24号',\n",
       "    'location': '113.677267,23.629149',\n",
       "    'direction': '西北',\n",
       "    'distance': '129.943'},\n",
       "   'businessAreas': [[]]},\n",
       "  'pois': [{'id': 'B00140MR9A',\n",
       "    'name': '广州南方学院',\n",
       "    'type': '科教文化服务;学校;高等院校',\n",
       "    'tel': '020-87828988;020-61787331',\n",
       "    'direction': '北',\n",
       "    'distance': '470.792',\n",
       "    'location': '113.679257,23.632577',\n",
       "    'address': '温泉大道882号',\n",
       "    'poiweight': '0.450402',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFG4TAF2',\n",
       "    'name': '广州南方公寓',\n",
       "    'type': '商务住宅;商务住宅相关;商务住宅相关',\n",
       "    'tel': '13622768437',\n",
       "    'direction': '东南',\n",
       "    'distance': '102.617',\n",
       "    'location': '113.678983,23.627778',\n",
       "    'address': '温泉镇乌土村松浪队5号(近博学路)',\n",
       "    'poiweight': '0.237309',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0H6FA18J5',\n",
       "    'name': '保利半山花园',\n",
       "    'type': '商务住宅;住宅区;住宅区',\n",
       "    'tel': [],\n",
       "    'direction': '东南',\n",
       "    'distance': '139.054',\n",
       "    'location': '113.679358,23.627672',\n",
       "    'address': '从莞深高速(广州南方学院南侧)',\n",
       "    'poiweight': '0.223065',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B00140TWHT',\n",
       "    'name': '教工住宅1号',\n",
       "    'type': '商务住宅;住宅区;住宅小区',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '222.658',\n",
       "    'location': '113.678490,23.630432',\n",
       "    'address': '笃行路2',\n",
       "    'poiweight': '0.157653',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFHRJDM9',\n",
       "    'name': '中南公寓',\n",
       "    'type': '商务住宅;住宅区;住宅小区',\n",
       "    'tel': '13711245345',\n",
       "    'direction': '东南',\n",
       "    'distance': '231.223',\n",
       "    'location': '113.680340,23.627566',\n",
       "    'address': '中大南方学院南门1号',\n",
       "    'poiweight': '0.256152',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGD588HJ',\n",
       "    'name': '广州南方学院教学楼4号',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '242.901',\n",
       "    'location': '113.677460,23.630490',\n",
       "    'address': '博学路',\n",
       "    'poiweight': '0.221662',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFHZH0IK',\n",
       "    'name': '地下铁',\n",
       "    'type': '餐饮服务;冷饮店;冷饮店',\n",
       "    'tel': [],\n",
       "    'direction': '西北',\n",
       "    'distance': '74.1376',\n",
       "    'location': '113.677941,23.629029',\n",
       "    'address': '博学路广州南方学院综合楼内',\n",
       "    'poiweight': '0.17934',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGDU76L2',\n",
       "    'name': '广州南方学院2号实验楼',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '280.003',\n",
       "    'location': '113.678211,23.630956',\n",
       "    'address': '博学路',\n",
       "    'poiweight': '0.221662',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFKP838K',\n",
       "    'name': '教学楼3号(外国语学院)',\n",
       "    'type': '科教文化服务;学校;高等院校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '298.093',\n",
       "    'location': '113.677247,23.630947',\n",
       "    'address': '博学路25',\n",
       "    'poiweight': '0.191742',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFKSF1FI',\n",
       "    'name': '香辣螺蛳粉',\n",
       "    'type': '餐饮服务;餐饮相关场所;餐饮相关',\n",
       "    'tel': '17820061022',\n",
       "    'direction': '西',\n",
       "    'distance': '209.206',\n",
       "    'location': '113.676231,23.628565',\n",
       "    'address': '广州南方学院西4123',\n",
       "    'poiweight': '0.209252',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGDU8NLD',\n",
       "    'name': '广州南方学院1号实验楼',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '326.007',\n",
       "    'location': '113.678215,23.631370',\n",
       "    'address': '博学路附近',\n",
       "    'poiweight': '0.221662',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFLF90I2',\n",
       "    'name': '煌上煌酱鸭(南方学院店)',\n",
       "    'type': '餐饮服务;快餐厅;快餐厅',\n",
       "    'tel': '13712317721',\n",
       "    'direction': '东北',\n",
       "    'distance': '256.772',\n",
       "    'location': '113.679965,23.630156',\n",
       "    'address': '笃行路20、22、24号广州南方学院商业中心1层1F015',\n",
       "    'poiweight': '0.209448',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGD58UTU',\n",
       "    'name': '广州南方学院2号教学楼',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '西北',\n",
       "    'distance': '346.352',\n",
       "    'location': '113.677060,23.631346',\n",
       "    'address': '博学路',\n",
       "    'poiweight': '0.191229',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0H25SMRKP',\n",
       "    'name': '广州南方学院大礼堂',\n",
       "    'type': '科教文化服务;会展中心;会展中心',\n",
       "    'tel': [],\n",
       "    'direction': '东北',\n",
       "    'distance': '386.65',\n",
       "    'location': '113.680076,23.631502',\n",
       "    'address': '温泉大道882号',\n",
       "    'poiweight': '0.261624',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGDU92JD',\n",
       "    'name': '广州南方学院教学楼1号',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '391.548',\n",
       "    'location': '113.677077,23.631783',\n",
       "    'address': '博学路19号',\n",
       "    'poiweight': '0.221662',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGD592BJ',\n",
       "    'name': '广州南方学院综合楼',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '东北',\n",
       "    'distance': '392.731',\n",
       "    'location': '113.680117,23.631544',\n",
       "    'address': '温泉大道886号广州南方学院内',\n",
       "    'poiweight': '0.221662',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFKP81F0',\n",
       "    'name': '教学楼1号(文学与传媒学院)',\n",
       "    'type': '科教文化服务;科教文化场所;科教文化场所',\n",
       "    'tel': [],\n",
       "    'direction': '西北',\n",
       "    'distance': '393.93',\n",
       "    'location': '113.676866,23.631736',\n",
       "    'address': '博学路19号附近',\n",
       "    'poiweight': '0.191742',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFLBEOND',\n",
       "    'name': '聚贤楼10号',\n",
       "    'type': '商务住宅;住宅区;宿舍',\n",
       "    'tel': [],\n",
       "    'direction': '东北',\n",
       "    'distance': '418.51',\n",
       "    'location': '113.681253,23.631036',\n",
       "    'address': '从莞深高速(广州南方学院)',\n",
       "    'poiweight': '0.190963',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFH6K3U9',\n",
       "    'name': '广州中大南方招待所',\n",
       "    'type': '住宿服务;住宿服务相关;住宿服务相关',\n",
       "    'tel': '020-61787222',\n",
       "    'direction': '西北',\n",
       "    'distance': '378.102',\n",
       "    'location': '113.676330,23.631332',\n",
       "    'address': '温泉镇温泉大道882号广州南方学院H7栋1-5层',\n",
       "    'poiweight': '0.254885',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGD58NF8',\n",
       "    'name': '广州南方学院达人书院',\n",
       "    'type': '科教文化服务;科教文化场所;科教文化场所',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '446.366',\n",
       "    'location': '113.679041,23.632392',\n",
       "    'address': '温泉大道882号广州南方学院新综合楼2层',\n",
       "    'poiweight': '0.191229',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFKUVYWV',\n",
       "    'name': '广州和风庭阿七度假别墅',\n",
       "    'type': '住宿服务;住宿服务相关;住宿服务相关',\n",
       "    'tel': [],\n",
       "    'direction': '东北',\n",
       "    'distance': '424.35',\n",
       "    'location': '113.681736,23.630569',\n",
       "    'address': '保利桃花源',\n",
       "    'poiweight': '0.273168',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGDU99RM',\n",
       "    'name': '广州南方学院图书馆',\n",
       "    'type': '科教文化服务;图书馆;图书馆',\n",
       "    'tel': '020-61787368;020-61787371',\n",
       "    'direction': '北',\n",
       "    'distance': '489.331',\n",
       "    'location': '113.679177,23.632762',\n",
       "    'address': '温泉大道882号广州南方学院内',\n",
       "    'poiweight': '0.257947',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGDU92JC',\n",
       "    'name': '广州南方学院4号实验楼',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '498.883',\n",
       "    'location': '113.679963,23.632652',\n",
       "    'address': '博学路附近',\n",
       "    'poiweight': '0.221662',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFL7C054',\n",
       "    'name': '广州U优森特酒店',\n",
       "    'type': '住宿服务;住宿服务相关;住宿服务相关',\n",
       "    'tel': '15813395296',\n",
       "    'direction': '北',\n",
       "    'distance': '464.819',\n",
       "    'location': '113.679226,23.632528',\n",
       "    'address': '温泉镇博学路882号(广州南方学院商业中心1层)',\n",
       "    'poiweight': '0.317505',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0FFG8CSEK',\n",
       "    'name': 'L5员工宿舍',\n",
       "    'type': '商务住宅;住宅区;宿舍',\n",
       "    'tel': [],\n",
       "    'direction': '东北',\n",
       "    'distance': '554.129',\n",
       "    'location': '113.682583,23.631487',\n",
       "    'address': '从莞深高速(广州南方学院)',\n",
       "    'poiweight': '0.185371',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGD58FR2',\n",
       "    'name': '广州南方学院教学楼9号',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '532.163',\n",
       "    'location': '113.679747,23.633032',\n",
       "    'address': '广州南方学院',\n",
       "    'poiweight': '0.221662',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HG5Z9JNK',\n",
       "    'name': '广州南方学院教学楼7号',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '544.868',\n",
       "    'location': '113.678600,23.633330',\n",
       "    'address': '博学路',\n",
       "    'poiweight': '0.221553',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGD599JM',\n",
       "    'name': '广州南方学院第11教学楼',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '556.036',\n",
       "    'location': '113.680037,23.633173',\n",
       "    'address': '温泉大道882号广州南方学院',\n",
       "    'poiweight': '0.221662',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0GUJYPEG2',\n",
       "    'name': '怒烤烤串(从化店)',\n",
       "    'type': '餐饮服务;中餐厅;特色/地方风味餐厅',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '510.112',\n",
       "    'location': '113.677160,23.632910',\n",
       "    'address': '温泉大道882号广州南方学院内博学路6号之3',\n",
       "    'poiweight': '0.216026',\n",
       "    'businessarea': []},\n",
       "   {'id': 'B0HGDU6Z41',\n",
       "    'name': '广州南方学院教学楼6号',\n",
       "    'type': '科教文化服务;学校;学校',\n",
       "    'tel': [],\n",
       "    'direction': '北',\n",
       "    'distance': '586.331',\n",
       "    'location': '113.678424,23.633710',\n",
       "    'address': '温泉大道882',\n",
       "    'poiweight': '0.221662',\n",
       "    'businessarea': []}],\n",
       "  'roads': [{'id': '020F49F0050461206',\n",
       "    'name': '博学路',\n",
       "    'direction': '北',\n",
       "    'distance': '7.63232',\n",
       "    'location': '113.678,23.6284'},\n",
       "   {'id': '020F49F0050462229',\n",
       "    'name': '老虎形街',\n",
       "    'direction': '北',\n",
       "    'distance': '445.159',\n",
       "    'location': '113.679,23.6245'},\n",
       "   {'id': '020F49F0050462451',\n",
       "    'name': '中山路',\n",
       "    'direction': '南',\n",
       "    'distance': '708.299',\n",
       "    'location': '113.677,23.6347'}],\n",
       "  'roadinters': [{'direction': '南',\n",
       "    'distance': '708.299',\n",
       "    'location': '113.676794,23.634661',\n",
       "    'first_id': '020F49F0050461206',\n",
       "    'first_name': '博学路',\n",
       "    'second_id': '020F49F0050462451',\n",
       "    'second_name': '中山路'}],\n",
       "  'aois': [{'id': 'B00140MR9A',\n",
       "    'name': '广州南方学院',\n",
       "    'adcode': '440117',\n",
       "    'location': '113.679257,23.632577',\n",
       "    'area': '528560.656426',\n",
       "    'distance': '0',\n",
       "    'type': '141201'}]}}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
